package com.mmdccj.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mmdccj.reggie.common.R;
import com.mmdccj.reggie.entity.Employee;
import com.mmdccj.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 登陆
     * @param session 浏览器session
     * @param employee json格式员工数据
     * @return 登陆结果
     */
    @PostMapping("/login")
    public R<Employee> login(HttpSession session, @RequestBody Employee employee) {

        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);
        log.info(emp.toString());
        if (emp.getUsername() == null) {
            return R.error("错误的用户名或密码");
        } else if (!emp.getPassword().equals(password)) {
            return R.error("错误的用户名或密码");
        }
        if (emp.getStatus() == 0) {
            return R.error("账号已禁用");
        }
        session.setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpSession session) {
        // 清除客户端session
        // session.removeAttribute("employee");
        // 完整清除当前session
        session.invalidate();
        return R.success("退出成功");
    }

    /**
     * 添加员工 ｜ 保存员工
     * @param employee 员工
     * @return 返回添加结果
     */
    @PostMapping
    public R<String> saveEmployee(HttpSession session,@RequestBody Employee employee){
        // 初始密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employeeService.save(employee);
        return R.success("添加成功");
    }

    /**
     * 员工分页查询
     * @param page 查询页码
     * @param pageSize 页码尺寸
     * @param name 若有则为查询的名字
     * @return 页面数据
     */
    @GetMapping("/page")
    public R<Page> getPages(int page, int pageSize,String name){
        Page pageInfo = new Page(page,pageSize);
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name), Employee::getUsername,name);
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        employeeService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 禁用或启用账户
     * @param session
     * @param employee
     * @return 更新结果
     */
    @PutMapping
    public R<String> update(HttpSession session,@RequestBody Employee employee){
        if(employee.getUsername().equals("admin")){
            return R.error("管理员无法被禁用");
        }
        employeeService.updateById(employee);
        return R.success("更新成功");
    }

    /**
     * 获取待编辑的用户
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getEmployeeData(@PathVariable long id){
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getId,id);
        Employee emp = employeeService.getOne(queryWrapper);
        return R.success(emp);
    }
}
